#include "AI7.h"
#include "AI7IsWin.c"


/*
 Look at the board and see if there are any favourable configurations that we want to
 work towards. Return points for configs that look promising. Like if we can get a combo
 of two blues or greens in a spot with an open piece on either end.
 */
int guess(char board[columns+2][rows+2])
{
    return 0;//no heuristics yet
}

int checkCol(char board[columns+2][rows+2], int col)
{  
    int i = 0;
   
    if(board[col][rows+1] != SPACE)
        return 0;//if the top of the col doesn't have a piece in it, it isn't full
    else
        for(i = rows+1; i > 0; i--)
        {
            if(board[col][i] == SPACE)
                continue;
            else
                return i;
        }
    return i+1;
}
// (8,7,5,0,0,0,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//g,s,s,s,s,s,s,
//g,b,s,s,s,s,s,
//s,s,s,s,s,s,s,
//b,g,s,s,s,s,s)

//Implements Minimax search - lowest level returns the min, then the upper levels take the
//max value of the returned mins
int minMax( char board[columns+2][rows+2], int depth)
{
    int max_depth = 5;
   
    max(board, 5);  
}

int max(char board[columns+2][rows+2], int depth){
   
    int best = -5, bestcol = 0;
    int val = 0;
    int i = 0;
    int row = 0;
   
    if(depth <= 0)
        return 0;
   
    for(i = 1; i < columns+1; i++)
    {
        if((row = checkCol(board, i)) == 0)
            continue;//col is full
        board[i][row] = BLUE;
        val = isWin(board, i) +  min(board, depth-1);
        if(val > best)
        {
            val = best;
            bestcol = i;
        }
       
        board[i][row] = GREEN;
        val = isWin(board, i) +  min(board, depth-1);
        if(val > best)
        {
            val = best;
            bestcol = i;
        }
       
    }
   
    return best;
   
   
}

int min(char board[columns+2][rows+2], int depth)
{
    int best = 5, bestcol = 0;
    int val = 0;
    int i = 0;
    int row = 0;
   
    if(depth <= 0)
        return 0;
   
    for(i = 1; i < columns+1; i++)
    {
        if((row = checkCol(board, i)) == 0)
            continue;//col is full
        board[i][row] = RED;
        val = isWin(board, i) +  max(board, depth-1) + guess(board);
        if(val < best)
        {
            val = best;
            bestcol = i;
        }
       
        board[i][row] = GREEN;
        val = isWin(board, i) +  max(board, depth-1) + guess(board);
        if(val < best)
        {
            val = best;
            bestcol = i;
        }
       
    }
   
    return best;
}

int tryMove(char board[columns+2][rows+2], int col, char piece)
{
    int i = 0, row = -1, depth = 5;
    // First, see if column has room for another piece
    printBoard(board);
   
    if((row = checkCol(board, col)) == 0)
        return -99999;
    else
    {
        board[col][row] = piece;
        //rank the moves by the number of points we can get to
        int score = 10000*isWin(board, col) + max(board, depth-1);
        board[col][row] = SPACE;
        return score;
    }
   
}

int makeMove(char board[columns+2][rows+2])
{
    int  i,tmp, max_move = -1, min_move = -1, max = 0, min = 0;
    char piece;
    // Loop through all possible moves
    for(i=1;i<=columns;i++)
    {
       
        // Try placing a Blue piece
        tmp = tryMove(board, i, BLUE);
#if DEBUG
        fprintf(f, "Trying col: %d - Piece: Blue - SCORE: %d\n\n", i, tmp);
#endif
        if (tmp > max)
        {
            max_move = i;
            piece = BLUE;
            max = tmp;               
        }
        // Try placing a Green piece
        tmp = tryMove(board, i, GREEN);
#if DEBUG
        fprintf(f, "Trying col: %d - Piece: Green - SCORE: %d\n\n", i, tmp);
#endif
        if (tmp > max)
        {
            max_move = i;
            piece = GREEN;
            max = tmp;               
        }
        else if (tmp < min)
        {
            if(tmp == -99999)//row is full
                continue;
            min_move = i;
            min = tmp;               
        }
        // Simulate the opponent placing Red here
        tmp = tryMove(board, i, RED);
        fprintf(f, "Trying col: %d - Piece: Red - SCORE: %d\n\n", i, tmp);
        if (tmp < min)
        {
            if(tmp == -99999)//row is full
                continue;
            min_move = i;
            min = tmp;               
        }                 
    }
    /*if(min < 0) // There's a Opponent's Win to Block
     fprintf(f, "(%d, %c)", min_move, BLUE);
     
     else
     */if(max > 0) // There's a Win
         printf("(%d, %c)", max_move, piece);
    if(min < 0)
        printf("(%d, b)", min_move, piece);
   
   
   
}


int checkLoss(char board[columns+2][rows+2])
{
    int i = 0, j = 0, row = 0, score = 0, max = 0, max_move = 0;
    char piece;
#if DEBUG
    printBoard(board);
#endif
   
    for(i = 1; i <= columns; i++)
    {
        // First, see if column has room for another piece
        for(j=1;j<=rows;j++)
        {
            if(board[i][j] == SPACE)
            {
                row = j;
                break;             
            }               
        }
        if (row == -1);
        else
        {
            board[i][row] = RED;
            //rank the moves by the number of points we can get to
            score = isWin(board, i);
#if DEBUG
            fprintf(f, "Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
           
            //if we are going to lose right away, place a blue to block;
            if(score < 0)
            {
                printf("(%d, b)", i);
                return 1;
            }
           
            board[i][row] = BLUE;
            //rank the moves by the number of points we can get to
            score = isWin(board, i);
#if DEBUG
            fprintf(f, "Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
           
            //if we are going to lose right away, place a blue to block;
            if(score > 0)
            {
                if(score > max)
                {
                    max_move = i;
                    max = score;
                    piece = BLUE;
                }
            }
           
            board[i][row] = GREEN;
            //rank the moves by the number of points we can get to
            score = isWin(board, i);
#if DEBUG
            fprintf(f, "Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
            board[i][row] = SPACE;
            if(score < 0)
            {
                printf("(%d, b)", i);
                return 1;
            }
            else if(score > max)
            {
                max_move = i;
                piece = GREEN;
                max = score;
            }
           
        }
        if(max)
        {
            printf("(%d, %c)", max_move, piece);
            return 1;
        }
    }
    return 0; 
}

//(10,7,5,0,0,0,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
//(10,7,7,2156,0,443,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,b,g,r,s,s,s,s,b,b,r,s,s,s,s,b,r,g,r,s,s,s,r,r,b,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
int main( int argc, char **argv)
{
   
    int p = 0;
#if DEBUG
    char fname[32];
#endif
   
    scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
#if DEBUG
    sprintf(fname, "./moves/game.out.%d.txt", player_1_time);
    f = fopen(fname, "w");
#endif
    char board[columns+2][rows+2];
    p = readBoard(board);
   
    if(checkLoss(board))
    {
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
   
   
    if(p == 0)
    {
        printf("(5, b)");
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
    else if(p < 2)
    {
        printf("(%d, b)", last_move+1);
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
    else if(p < 6)
    {
        printf("(%d, b)", last_move);
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
   
    makeMove(board);
#if DEBUG
    fclose(f);
#endif
    return 0;
}